package com.example.demo.controller;

import com.example.demo.entity.SysPermission;
import com.example.demo.entity.SysRole;
import com.example.demo.repository.SysPermissionRepository;
import com.example.demo.repository.SysRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright(C), 2020-2021, AmbroseCdMeng
 * FileName:    SysPermissionController
 * Author:      AmbroseCdMeng
 * Date:        2021/3/5 9:22
 * Description: 权限控制器
 * History:
 * <author>     <time>      <version>       <desc>
 * 作者姓名     修改时间        版本号           描述
 */
@Controller
@RequestMapping("/admin/permission")
public class SysPermissionController {
    @Autowired
    private SysPermissionRepository sysPermissionRepository;

    @Autowired
    private SysRoleRepository sysRoleRepository;

    @RequestMapping("/add")
    public String addPermission(Model model){
        Iterable<SysRole> sysRoles = sysRoleRepository.findAll();
        model.addAttribute("sysRole", sysRoles);
        return "admin/permission/add";
    }

    @PostMapping("/add")
    public String addPermission(SysPermission sysPermission, String role){
        List<SysRole> roles = new ArrayList<>();
        SysRole r1 = sysRoleRepository.findByRole(role);
        roles.add(r1);
        sysPermission.setRoles(roles);
        sysPermissionRepository.save(sysPermission);
        return "redirect:/admin/";
    }
}
